<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>promise实现图片的异步加载</title>
</head>

<body>
    <script>
        function imageAsync(url) {
            return new Promise((resolve, reject) => {
                let img = new Image();
                img.src = url;
                img.onload = function (image) {
                    console.log('图片请求成功，此处进行通用操作');
                    resolve(image);
                }
                img.onerror = function (error) {
                    console.log('失败，图片请求失败');
                    reject(error);
                }
            })
        }

        imageAsync(url).then(() => {
            console.log('加载成功');
        }).catch(err => {
            console.log('加载失败');
        })
    </script>
</body>

</html>